from pydoc import html
import requests as req
import json

# 用于维持登录会话，requests高级用法
s = req.Session()

# 登录
def login():
    # 豆瓣的登录url
    loginurl = 'https://accounts.douban.com/j/mobile/login/basic'
    # 设置headers
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) \
            AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 \
            Safari/537.36'}
    # 表单提交参数设置
    data = {
    'ck':'',
    'name':'用户',
    'password':'密码',
    'remember':'false',
    'ticket':''
    }
    html = s.post(loginurl,headers=headers,data=data)
    return s,html

# 获取个人信息
def get_user_data(s):
    url = 'https://www.douban.com/people/57443010/'
    html = s.get(url)
    print(html)
    html = html.text
    return html

if __name__ == '__main__':
    s,html1 = login()
    print(s)
    print(html1.status_code)
    html2 = get_user_data(s)


# # get请求的request
# req_get = urllib.request.Request(url,headers=headers)
# # post请求的request
# req_post = urllib.request.Request(loginurl,urllib.parse.urlencode(data).encode('utf-8'),headers)
# # get请求的response
# res_get = opener.open(req_get)
# # post请求的response
# res_post = opener.open(req_post)

# res_html = res_post.read().decode('utf-8')

# # json解码
# res_js = json.loads(res_html)
# # 根据status的值判断是否登录成功
# if res_js['status'] == 'success':
#     print('登录成功')
# else:
#     print('登录失败')
